package com.example.chatfree.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.example.chatfree.R;

/**
 * 用户登录页面
 * Created by 周国谱 on 2015/10/8.
 */
public class LoginActivity extends BaseActivity {
    private Context mContext;
    private Button mBtnLogin;
    private EditText mEdtName, mEdtPassword;
    private TextView mTvForgetPwd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = LoginActivity.this;
        initView();
    }

    /**
     * 初始化页面
     */
    private void initView() {
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mEdtName = (EditText) findViewById(R.id.edt_name);
        mEdtPassword = (EditText) findViewById(R.id.edt_password);
        mTvForgetPwd = (TextView) findViewById(R.id.tv_forget_password);

        mBtnLogin.setOnClickListener(new clickListener());
        mTvForgetPwd.setOnClickListener(new clickListener());
    }

    /**
     * 用户登录
     *
     * @param userName 用户名
     * @param password 密码
     */
    private void login(String userName, String password) {
        showProgressDialog(getString(R.string.loading));
        //登录
        AVUser.logInInBackground(userName,password,
                new LogInCallback() {
                    public void done(AVUser user, AVException e) {
                        if (user != null) { //登录成功
                            dismissProgressDialog();
                            showToastMessage(getString(R.string.login)+getString(R.string.success),mContext);
                            Intent mainIntent = new Intent(mContext,
                                    MainActivity.class);
                            startActivity(mainIntent);
                            finish();
                        } else { //登录失败
                           dismissProgressDialog();
                            showToastMessage(e.getMessage(),mContext);
                        }
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_register:
                Intent intent = new Intent(mContext, RegisterActivity.class);
                startActivity(intent);
                break;
        }
        return true;
    }

    /**
     * 页面控件点击事件
     */
    class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_login:  //登录
                    String userName = mEdtName.getText().toString().trim();
                    String password = mEdtPassword.getText().toString().trim();
                    if (userName == null || userName.equals("")) {
                        Toast.makeText(mContext, getString(R.string.null_user_name), Toast.LENGTH_SHORT).show();
                    } else if (password == null || password.equals("")) {
                        Toast.makeText(mContext, getString(R.string.null_password), Toast.LENGTH_SHORT).show();
                    } else {
                        login(userName, password);
                    }

                    break;
                case R.id.tv_forget_password:  //忘记密码
                    break;
            }
        }
    }
}
